<template>
  <el-upload
    class="ed-file-upload"
    :action="action"
    :show-file-list="false"
    v-bind="$attrs"
    :on-success="success"
  >
    <el-button
      size="small"
      type="primary"
    >
      {{ tip }}
    </el-button>
  </el-upload>
</template>

<script>
export default {
  props: {
    action: {
      type: String,
      default: ''
    },
    value: {
      type: String,
      default: null
    },
    /**
     * {function} onSuccess
     * @param {object} response
     */
    onSuccess: {
      type: Function,
      default: null
    },
    preloadLabel: {
      type: String,
      default: '点击上传'
    },
    completeLabel: {
      type: String,
      default: '已上传'
    }
  },
  computed: {
    tip () {
      return (this.value) ? this.completeLabel : this.preloadLabel
    }
  },
  methods: {
    success (response) {
      if (response.url) {
        this.$emit('change', response.url)
        if (this.onSuccess) (this.onSuccess)(response)
      }
    }
  }
}
</script>

<style scoped>

</style>
